<!--
 * @Author: Olivia
 * @Date: 2022-12-04 17:42:10
 * @custom_string_Olivia: Less is more
-->
<template>
    <van-icon
    :color="value ? 'pink' : '' "
    :name="value ? 'star' : 'star-o' "
    :loading="loading"
    @click="onCollect"
    />
</template>
<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'CollectArticle',
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  created () {
  },
  computed: {
  },
  methods: {
    async onCollect () {
      // 开始请求
      this.loading = true
      try {
        if (this.value) {
          await deleteCollect(this.articleId)
        } else {
          await addCollect(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功' : '取消成功')
      } catch (error) {
        this.$toast('操作失败，请重试')
      }
      // 请求结束
      this.loading = false
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
